﻿/**
 * This is a part of MCServe, which is available under the Apache license 2.0.
 * With this code, you may do whatever you want as long as that is not in conflict with this license.
 * In addition, you should have a reference to the original project to include (http://mcserve.codeplex.com).
 * 
 * Author       : Lars Krämer ( Xe77a )
 * Website      : http://www.lkraemer.de
 * Project      : MCServe (http://mcserve.codeplex.com)
 * Copyright    : Copyright (c) 2013 lkraemer.de
 * License      : http://mcserve.codeplex.com/license
 * Description  : This is a part of the core functionality of the server
 */

using MCServeLib.Types;
using System;
using System.ComponentModel;
using System.IO;

namespace MCServeCore.Components
{
    public class ServerConsole : INotifyPropertyChanged
    {
        public enum OutputTypes
        {
            Error = 0,                      // Error-Message    red
            Warning = 1,                    // Warning          orange
            Information = 2,                // Information      blue
            Message = 3,                    // Normal Text      white / black
            Command = 4                     // Execute the string in the server-console
        }
        public enum OutputTarget
        {
            Console = 0,                    // Print it only to the Console
            ErrorLog = 1,                   // Print it only to the error-log
            MessageLog = 2,                 // Print it only to the message-log
            WarningLog = 3,                 // Print it only to the warning-log
            ErrorLogAndConsole = 4,         // Print it only to the error-log and to the console
            MessageLogAndConsole = 5,       // Print it only to the message-log and to the console
            WarningLogAndConsole = 6        // Print it only to the warning-log and to the console
        }

        public event PropertyChangedEventHandler PropertyChanged;
        protected virtual void OnPropertyChanged(PropertyChangedEventArgs e)
        {
            if (PropertyChanged != null)
                PropertyChanged(this, e);
        }

        private string _Content = "";
        public string Content
        { 
            get
            {
                return _Content;
            }
            set
            {
                if (value != _Content)
                {
                    _Content = value;
                    OnPropertyChanged(new PropertyChangedEventArgs("Content"));
                }
            }
        }

        public bool SendMessage(String Message)
        {
            return SendMessage(Message, OutputTarget.MessageLogAndConsole, OutputTypes.Message);
        }

        public bool SendMessage(String Message, OutputTarget Target = OutputTarget.Console, OutputTypes Output = OutputTypes.Message)
        {
            /**
             * Prints the Message into the Console
             */
            StreamWriter LogWriter = null;

            /**
             * #######################################################################
             * CONSOLE
             * #######################################################################
             */
            if (Target == OutputTarget.Console ||
                Target == OutputTarget.ErrorLogAndConsole ||
                Target == OutputTarget.MessageLogAndConsole ||
                Target == OutputTarget.WarningLogAndConsole)
            {
                this.Content = this.Content + "[" + Output.ToString().ToUpper() + "]\t" + Message + '\n';
            }

            /**
             * #######################################################################
             * ERROR LOG
             * #######################################################################
             */
            if ((Target == OutputTarget.ErrorLog ||
                Target == OutputTarget.ErrorLogAndConsole) &&
                Output == OutputTypes.Error)
            {
                LogWriter = new StreamWriter(GameServer.Paths.Get(MCServePaths.LOG_ERRORS), true);
                LogWriter.Write("[" + Output.ToString().ToUpper() + "]\t[" + DateTime.Now + "]\t" + Message + '\n');
                LogWriter.Close();
            }

            /**
             * #######################################################################
             * MESSAGE LOG
             * #######################################################################
             */
            if ((Target == OutputTarget.MessageLog ||
                Target == OutputTarget.MessageLogAndConsole) && (
                Output == OutputTypes.Information ||
                Output == OutputTypes.Message))
            {
                LogWriter = new StreamWriter(GameServer.Paths.Get(MCServePaths.LOG_MESSAGES), true);
                LogWriter.Write("[" + Output.ToString().ToUpper() + "]\t[" + DateTime.Now + "]\t" + Message + '\n');
                LogWriter.Close();
            }

            /**
             * #######################################################################
             * WARNING LOG
             * #######################################################################
             */
            if ((Target == OutputTarget.WarningLog ||
                Target == OutputTarget.WarningLogAndConsole) &&
                Output == OutputTypes.Warning)
            {
                LogWriter = new StreamWriter(GameServer.Paths.Get(MCServePaths.LOG_WARNING), true);
                LogWriter.Write("[" + Output.ToString().ToUpper() + "]\t[" + DateTime.Now + "]\t" + Message + '\n');
                LogWriter.Close();
            }

            /**
             * #######################################################################
             * COMMAND
             * #######################################################################
             */
            if (Output == OutputTypes.Command)
            {
                // EXECUTE THE COMMAND
                //CommandExecuting.ExecuteCommand(Message);
            }

            LogWriter = new StreamWriter(GameServer.Paths.Get(MCServePaths.LOG_SERVER), true);
            LogWriter.Write("[" + Output.ToString().ToUpper() + "]\t[" + DateTime.Now + "]\t" + Message + '\n');
            LogWriter.Close();
            return true;
        }
    }
}
